#!/bin/bash

# 录取概率计算性能测试脚本
# 用于对比优化前后的性能差异

echo "=== 录取概率计算性能测试 ==="
echo "测试时间: $(date)"
echo ""

# 测试参数
BASE_URL="http://localhost:8080"
API_ENDPOINT="/admin-api/system/college-recommendation/recommend"

# 测试用例1: 小数据量测试
echo "1. 小数据量测试 (页面大小: 20)"
echo "-----------------------------------"

# 构建请求参数
SMALL_DATA_PARAMS="province=辽宁&score=580&ranks=15000&year=2024&classify=物理类&subjects=物理,化学,生物&batch=本科批&pageNum=1&pageSize=20"

# 执行测试
echo "发送请求..."
START_TIME=$(date +%s%3N)
curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${SMALL_DATA_PARAMS}" > /dev/null
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))
echo "响应时间: ${DURATION}ms"
echo ""

# 测试用例2: 中等数据量测试
echo "2. 中等数据量测试 (页面大小: 50)"
echo "-----------------------------------"

MEDIUM_DATA_PARAMS="province=辽宁&score=550&ranks=25000&year=2024&classify=物理类&subjects=物理,化学&batch=本科批&pageNum=1&pageSize=50"

echo "发送请求..."
START_TIME=$(date +%s%3N)
curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${MEDIUM_DATA_PARAMS}" > /dev/null
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))
echo "响应时间: ${DURATION}ms"
echo ""

# 测试用例3: 大数据量测试
echo "3. 大数据量测试 (页面大小: 100)"
echo "-----------------------------------"

LARGE_DATA_PARAMS="province=辽宁&score=520&ranks=35000&year=2024&classify=物理类&subjects=物理&batch=本科批&pageNum=1&pageSize=100"

echo "发送请求..."
START_TIME=$(date +%s%3N)
curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${LARGE_DATA_PARAMS}" > /dev/null
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))
echo "响应时间: ${DURATION}ms"
echo ""

# 测试用例4: 缓存命中测试
echo "4. 缓存命中测试 (重复请求)"
echo "-----------------------------------"

echo "第一次请求 (缓存未命中)..."
START_TIME=$(date +%s%3N)
curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${SMALL_DATA_PARAMS}" > /dev/null
END_TIME=$(date +%s%3N)
DURATION1=$((END_TIME - START_TIME))
echo "第一次响应时间: ${DURATION1}ms"

echo "第二次请求 (缓存命中)..."
START_TIME=$(date +%s%3N)
curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${SMALL_DATA_PARAMS}" > /dev/null
END_TIME=$(date +%s%3N)
DURATION2=$((END_TIME - START_TIME))
echo "第二次响应时间: ${DURATION2}ms"

IMPROVEMENT=$((DURATION1 - DURATION2))
echo "缓存优化效果: ${IMPROVEMENT}ms (减少 $((IMPROVEMENT * 100 / DURATION1))%)"
echo ""

# 测试用例5: 并发测试
echo "5. 并发测试 (5个并发请求)"
echo "-----------------------------------"

echo "启动并发请求..."
START_TIME=$(date +%s%3N)

# 启动5个并发请求
for i in {1..5}; do
    curl -s -X GET "${BASE_URL}${API_ENDPOINT}?${SMALL_DATA_PARAMS}" > /dev/null &
done

# 等待所有请求完成
wait

END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))
echo "并发测试完成时间: ${DURATION}ms"
echo "平均每个请求时间: $((DURATION / 5))ms"
echo ""

echo "=== 性能测试完成 ==="
echo "测试建议:"
echo "1. 第一次请求时间较长是正常的（数据库查询 + 缓存预热）"
echo "2. 后续请求应该明显更快（缓存命中）"
echo "3. 大数据量请求可能启用多线程处理"
echo "4. 并发请求应该能正常处理，响应时间相对稳定"
echo ""
echo "如需详细的性能分析，请查看应用日志中的性能统计信息。"
